<?php

namespace app\models;

use Yii;
use app\models\Profile;

/**
 * This is the model class for table "{{%user}}".
 *
 * @property string $userid
 * @property string $username
 * @property string $userpass
 * @property string $useremail
 * @property string $createtime
 */
class User extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public $respass;
    public $rememberMe=true;

    public static function tableName()
    {
        return '{{%user}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['username','required','message'=>'用户名不能为空','on'=>['useradd','login']],
            [['userpass'], 'required','message'=>'用户密码不能为空','on'=>['useradd','login']],
            ['userpass','validatePass','on'=>['login']],
            [['respass'], 'required','message'=>'确认密码不能为空','on'=>['useradd']],
            [['useremail'], 'email', 'message'=>'邮箱格式不正确','on'=>['useradd','regbymail']],
            ['respass','compare','compareAttribute'=>'userpass','message'=>'两次密码输入不一致','on'=>['useradd']],
            ['username','unique','message'=>'用户名已经被注册','on'=>['useradd']],
            ['useremail','unique','message'=>'电子邮箱已经被注册','on'=>['useradd','regbymail']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'userid' => '用户id',
            'username' => '用户名',
            'userpass' => '密码',
            'useremail' => '电子邮箱',
            'respass'=>'确认密码',
            'createtime' => 'Createtime',
            'rememberMe'=>'记住我',
        ];
    }

    public function validatePass()
    {
        if(!$this->hasErrors()){
            $data= self::find()->where('username=:user and userpass=:pass',[':user'=> $this->username,':pass'=> md5($this->userpass)])->one();
            if(is_null($data)){
                $this->addError('userpass','用户名和密码不正确');
            }
        }
    }

        //关联查询所用
    public function getProfile()
    {
        return $this->hasOne(Profile::className(), ['userid'=>'userid']);
    }

    //添加用户
    public function reg($data,$scenario = 'useradd')
    {
        $this->scenario=$scenario;
        if($this->load($data) && $this->validate()){
            $this->createtime= time();
            $this->username=$data['User']['username'];
            $this->userpass= md5($data['User']['userpass']);
            if($this->save(false)){
                return true;
            }
            return false;
        }
        return false;
    }
    
    //登陆
    public function login($data)
    {
        $this->scenario='login';
        if($this->load($data) && $this->validate()){
            $lifetime= $this->rememberMe ? 24*3600 : 0;
            $session= Yii::$app->session;
            session_set_cookie_params($lifetime);
            $session['User']=[
                'username'=> $this->username,
                'isLogin'=>'1',
            ];
            return (bool)$session['isLogin'];
        }
        return false;
    }




    //发送电子邮件
    public function regByMail($data)
    {
        $data['User']['username']='imooc_'.uniqid();
        $data['User']['userpass']= uniqid();
        $this->scenario='regbymail';
        if($this->load($data) && $this->validate()){
            $mail= Yii::$app->mailer->compose('creatuser',['username'=>$data['User']['username'],'userpass'=>$data['User']['userpass']]);
            $mail->setFrom('xinlanforeverk@163.com');
            $mail->setTo($data['User']['useremail']);
            $mail->setSubject('新兰永恒-慕课商城，新建用户');
            if($mail->send() && $this->reg($data,'regbymail')){
                return true;
            }
        }
        return false;
    }
    
    
    
    
}
